package com.nec.recommend.common;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.injector.methods.Update;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 基本实体类,该类提供了一张数据表中最基本的数据
 *
 * @author limingzhen
 * @since 2024/4/7
 */
public class BaseEntity implements Serializable {

    /**
     * 主键(雪花)
     */
    @TableId(type = IdType.ASSIGN_ID)
    @NotNull(message = "主键不得为空", groups = {Update.class})
    private Long id;

    /**
     * 创建日期
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 创建人
     */
    @TableField(fill = FieldFill.INSERT)
    private Long createUser;

    /**
     * 最后修改日期
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 最后修改人
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;

    /**
     * 数据状态
     */
    @TableField(fill = FieldFill.INSERT)
    private Integer status;

    /**
     * 删除状态 0:未删除 1:已删除
     */
    @TableLogic
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Integer deleteStatus;

    public BaseEntity(Long id, LocalDateTime createTime, Long createUser, LocalDateTime updateTime, Long updateUser, Integer status, Integer deleteStatus) {
        this.id = id;
        this.createTime = createTime;
        this.createUser = createUser;
        this.updateTime = updateTime;
        this.updateUser = updateUser;
        this.status = status;
        this.deleteStatus = deleteStatus;
    }

    public BaseEntity() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public Long getCreateUser() {
        return createUser;
    }

    public void setCreateUser(Long createUser) {
        this.createUser = createUser;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public Long getUpdateUser() {
        return updateUser;
    }

    public void setUpdateUser(Long updateUser) {
        this.updateUser = updateUser;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getDeleteStatus() {
        return deleteStatus;
    }

    public void setDeleteStatus(Integer deleteStatus) {
        this.deleteStatus = deleteStatus;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof BaseEntity)) {
            return false;
        }
        BaseEntity that = (BaseEntity) o;
        return getId().equals(that.getId()) &&
                getCreateTime().equals(that.getCreateTime()) &&
                getCreateUser().equals(that.getCreateUser()) &&
                getUpdateTime().equals(that.getUpdateTime()) &&
                getUpdateUser().equals(that.getUpdateUser()) &&
                getStatus().equals(that.getStatus()) &&
                getDeleteStatus().equals(that.getDeleteStatus());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getCreateTime(), getCreateUser(), getUpdateTime(), getUpdateUser(), getStatus(), getDeleteStatus());
    }

    @Override
    public String toString() {
        return "BaseEntity{" +
                "id=" + id +
                ", createTime=" + createTime +
                ", createUser=" + createUser +
                ", updateTime=" + updateTime +
                ", updateUser=" + updateUser +
                ", status=" + status +
                ", deleteStatus=" + deleteStatus +
                '}';
    }
}
